﻿using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.SalesCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.SalesCommandHandler
{
    /// <summary>
    /// 查询发货通知单命令处理
    /// </summary>
    public class GetDeliveryNoticeCommandHandler : IRequestHandler<GetDeliveryNoticeCommand, APIResult<List<DeliveryDTO>>>
    {
        private readonly IBaseRepository<DeliveryNotice> delivRep;
        private readonly IBaseRepository<OrderMaterials> orderRep;
        public GetDeliveryNoticeCommandHandler(IBaseRepository<DeliveryNotice> delivRep, IBaseRepository<OrderMaterials> orderRep)
        { 
            this.orderRep = orderRep;
            this.delivRep = delivRep;
        }

        public Task<APIResult<List<DeliveryDTO>>> Handle(GetDeliveryNoticeCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<DeliveryDTO>> result = new APIResult<List<DeliveryDTO>>();
            result.Code = APIEnum.成功;
            result.Msg=APIEnum.查询成功.ToString();
            var list = from a in delivRep.GetAll().Result
                       join b in orderRep.GetAll().Result on a.NoticeId equals b.SaleCode
                       where a.IsDeleted == false
                       select new DeliveryDTO
                       {
                           NoticeId=a.NoticeId,
                           DocumentCode = a.DocumentCode,
                           DocumentDate = a.DocumentDate,
                           BusinessTypeId = a.BusinessTypeId,
                           WarehouseId = a.WarehouseId,
                           Consignee = a.Consignee,
                           Operator = a.Operator,
                           CustomerName = a.CustomerName,
                           Phone = a.Phone,
                           DocumentStatus = a.DocumentStatus,
                           Auditor = a.Auditor,
                           AuditDate = a.AuditDate,
                           DeliveryDate = a.DeliveryDate,
                           FreightCost = a.FreightCost,
                           BusinessStatusId = a.BusinessStatusId,
                           HandlingFee = a.HandlingFee,
                           Address = a.Address,
                           Remark = a.Remark,
                           Id=b.Id,
                           SaleCode=b.SaleCode,
                           MaterialsCode = b.MaterialsCode,
                           MaterialsName = b.MaterialsName,
                           ModelSpecification = b.ModelSpecification,
                           Calulate = b.Calulate,
                           CalulateNumber = b.CalulateNumber,
                           OutBoundNumber = b.OutBoundNumber,
                           PackRequir = b.PackRequir
                       };
            if(!string.IsNullOrEmpty(request.DocumentCode))
            {
                list = list.Where(x => x.DocumentCode.Contains(request.DocumentCode));
            }
            if (!string.IsNullOrEmpty(request.CustomerName))
            {
                list = list.Where(x => x.CustomerName.Contains(request.CustomerName));
            }
            list = list.OrderByDescending(x => x.NoticeId);
            result.data = list.ToList();
            return Task.FromResult(result);
        }
    }
}
